package hluck.me.srorage.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hluck.me.srorage.sys.domain.SysUser;
import hluck.me.srorage.sys.mapper.SysRoleMapper;
import hluck.me.srorage.sys.mapper.SysUserMapper;
import hluck.me.srorage.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 老雷
 * @since 2021-02-15
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
   @Autowired
   private SysRoleMapper roleMapper;

    @Override
    public boolean save(SysUser entity) {
        return super.save(entity);
    }

    @Override
    public SysUser getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public boolean updateById(SysUser entity) {
        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        //根据用户id删除用户角色中间表的数据
        roleMapper.deleteRoleUserByUid(id);
        //删除用户头像[如果是默认头像就不删除，否则删除]
        return super.removeById(id);
    }

    /**
     * 保存用户和角色的关系
     * @param uid
     * @param ids
     */
    @Override
    public void saveUserRole(Integer uid, Integer[] ids) {
        //根据用户id删除sys_role_user里面的数据
        this.roleMapper.deleteRoleUserByUid(uid);
        if (ids!=null&&ids.length>0) {
            for (Integer rid : ids) {
                this.roleMapper.insertUserRole(uid,rid);
            }
        }
    }
}
